package jmu.edu.yjc.controller;

import com.alibaba.fastjson.JSONObject;
import jmu.edu.yjc.domain.ResponseResult;
import jmu.edu.yjc.domain.dto.MessageDto;
import jmu.edu.yjc.domain.entity.Message;
import jmu.edu.yjc.service.MailInfoService;
import jmu.edu.yjc.service.MessageService;
import jmu.edu.yjc.service.WebSocketService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@RestController
@RequestMapping("/websocket")
public class WebSocketController {
    @Resource
    private WebSocketService webSocketService;
    @Resource
    private MessageService messageService;
    @Resource
    private MailInfoService mailInfoService;
    @GetMapping("/message/getContacts")
    public ResponseResult getContacts(Long id){
        return webSocketService.getToUserContracts(id);
    }
    @GetMapping("/messages")
    public ResponseResult getMessages(Integer pageNumber,Integer pageSize,Long userId,Long toUserId){
        return webSocketService.getMessages(pageNumber,pageSize,userId,toUserId);
    }
    @GetMapping("/message/totalUnread")
    public ResponseResult getTotalUnread(Long id){
        return webSocketService.getTotalUnRead(id);
    }
    @PutMapping("/message/updateMessageCheck")
    public ResponseResult updateMessageCheck(Long fromUserId,Long toUserId){
        return webSocketService.updateMessagesByUserId(fromUserId,toUserId);
    }
    @PutMapping("/message/updateMessageByID")
    public ResponseResult updateMessageByID(String messageId){
        return webSocketService.updateMessageById(messageId);
    }
    @PostMapping("/message/addMessage")
    public ResponseResult addMessage(@RequestBody MessageDto dto){
        return messageService.addMessage(dto.getMessage());
    }
    @PostMapping("/message/uploadFile")
    public ResponseResult uploadFile(@RequestParam("file") MultipartFile multipartFile,
                                     @RequestParam("message") String message){
        return webSocketService.uploadFile(multipartFile , message);
    }
    @GetMapping("/message/unreadContract/{id}")
    public ResponseResult getUnreadContract(@PathVariable("id")Long id){
        return messageService.getUnreadContractByID(id);
    }
    @GetMapping("/message/updateReadStatus/{hid}")
    public ResponseResult updateReadStatus(@PathVariable("hid")Long hid) {
        return mailInfoService.updateReadStatus(hid);
    }
    @GetMapping("/message/listMailInfo/{id}")
    public ResponseResult listMailInfo(@PathVariable("id")Long id) {
        return mailInfoService.listMailInfo(id);
    }

}
